package com.qk.mangement.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qk.common.PageResult;
import com.qk.dto.role.RoleDTo;
import com.qk.entity.Role;
import com.qk.mangement.mapper.RoleMapper;
import com.qk.mangement.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Date:2025/10/9
 * Author:zxc
 * Description
 */
@Slf4j
@Service
public class RoleServicelmpl implements RoleService {


    @Autowired
    private RoleMapper roleMapper;


    @Override
    public PageResult<Role> queryListByPage(RoleDTo roleDTO) {
        PageInfo<Role> pageInfo = PageHelper.startPage(roleDTO.getPage(), roleDTO.getPageSize())
                .doSelectPageInfo(() -> roleMapper.queryPage(roleDTO));
        return PageResult.<Role>builder().total(Long.valueOf(pageInfo.getTotal()).intValue()).rows(pageInfo.getList()).build();

    }

    @Override
    public void deleteById(Integer id) {
        roleMapper.deleteById(id);

    }

    @Override
    public void addUser(Role role) {
        boolean hasNullField = BeanUtil.hasNullField(role, "id", "reamrk", "createTime", "updateTime");
        if(hasNullField){
            throw new RuntimeException("参数错误");
        }

        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.insert(role);
        log.info("添加角色：{}", role);

    }

    @Override
    public Object getById(Integer id) {
        Role role = roleMapper.getById(id);
        return role;

    }

    @Override
    public void update(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.update(role);
        log.info("修改角色：{}", role);

    }


    @Override
    public List<Role> queryAll(){
        log.info("查询所有角色");
        List<Role> roleList=roleMapper.queryAll();
        return roleList;

    }

  }
